package com.simtop.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.exchangepo.Question;
import com.simtop.pojo.userpo.User;
import com.simtop.service.KsServer;
import com.simtop.service.SjServer;
import com.simtop.util.jwtutil.JwtUtil;
import com.simtop.vo.exam.ExamVos;
import com.simtop.vo.ksvo.Ksvo;
import com.simtop.vo.ksvo.Ksvos;
import com.simtop.vo.ksvo.Ksvoss;
import com.simtop.vo.sj.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.simtop.common.Resources.Http;


/**
 * @ClassName: KsController
 * @Description TODO 考试控制
 * @Author liuzhaobo
 * @Date 2022/10/21 0021 19:27
 * @Version 1.0
 **/

@RestController
@CrossOrigin
@RequestMapping("/ks")
public class KsController {
    @Autowired
    private SjServer sjServer;

    @Autowired
    private KsServer ksServer;


    /**
     * @Author: liuzhaobo
     * @Description: 学生考试 废弃
     * 会生成属于他的 这个时间的  一份试卷
     * 这个试卷对应试题记录表
     * @DateTime: 2022/10/21 0021 19:33
     * @Params:
     * @Return
     */
    @RequestMapping("/createStuExam")
    @ResponseBody
    public ServerResponse<Object> createStuExam(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            //生成 一条记录
            JSONObject entries = JSONUtil.parseObj(data);
            Integer examId = entries.getInt("examId");
            Integer id = u.getId();
            String username = u.getUsername();
            Ksvo ksvo = new Ksvo();
            ksvo.setUserid(id);
            ksvo.setExamId(examId);
            ksvo.setUsername(username);

            ksServer.createStuExam(ksvo);
            Integer ids = ksvo.getId();
            //返回ids 和 题目 list
            List<sjVo> findStuExamSj = sjServer.findStuExamSj(examId);
            for (sjVo as : findStuExamSj
            ) {
                as.setIds(ids);
                as.setExamIds(examId);
            }


            return ServerResponse.createBySuccess(findStuExamSj);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("创建失败");


        }


    }


    /**
     * @Author: liuzhaobo
     * @Description: 学生考试
     * 会生成属于他的 这个时间的  一份试卷
     * 这个试卷对应试题记录表
     * @DateTime: 2022/10/21 0021 19:33
     * @Params:
     * @Return
     */
    @RequestMapping("/createStuExam2")
    @ResponseBody
    public ServerResponse<Object> createStuExam2(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            //生成 一条记录
            JSONObject entries = JSONUtil.parseObj(data);
            Integer examId = entries.getInt("examId");
            Date createTime = entries.getDate("createTime");
            Date endTime = entries.getDate("endTime");
            Integer id = u.getId();
            //进入考核验证
            //先获取 考核   1   2
            //根据 userid 和   examid 获取次数
            // Integer assessMent = ksServer.getassessMent(examId);


            String username = u.getUsername();
            Ksvo ksvo = new Ksvo();
            ksvo.setUserid(id);
            ksvo.setExamId(examId);
            ksvo.setUsername(username);
            ksvo.setCreateTime(createTime);
            ksvo.setEndTime(endTime);
            ksServer.createStuExam(ksvo);
            Integer ids = ksvo.getId();
            //返回ids 和 试卷 id
            Ksvoss ksvoss = new Ksvoss();
            ksvoss.setId(ids);
            ksvoss.setExamId(examId);

            return ServerResponse.createBySuccess(ksvoss);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("创建失败");


        }


    }

    /**
     * @Author: liuzhaobo
     * @Description: 查看产生的试卷
     * @DateTime: 2022/10/22 0022 10:02
     * @Params:
     * @Return
     */
    @RequestMapping("/findStuExam")
    @ResponseBody
    public ServerResponse<Object> findStuExam(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            Integer pageNum = entries.getInt("pageNum");
            Integer pageSize = entries.getInt("pageSize");
            String px = entries.getStr("px");
            String op = entries.getStr("op");
            String examName = entries.getStr("examName");
            if (u.getRoleId() == 1 || u.getRoleId() == 2) {
                //查看所有
                PageHelper.startPage(pageNum, pageSize);
                List<Ksvos> finAllOne = ksServer.finAllOne(op, px, examName);
                PageInfo<Ksvos> pageInfo = new PageInfo<Ksvos>(finAllOne);
                return ServerResponse.createBySuccess(pageInfo);
            } else {
                PageHelper.startPage(pageNum, pageSize);
                Integer id = u.getId();
                List<Ksvos> finAllOne = ksServer.finAllTwo(op, px, id, examName);
                PageInfo<Ksvos> pageInfo = new PageInfo<Ksvos>(finAllOne);
                return ServerResponse.createBySuccess(pageInfo);
            }


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查看失败");
        }
    }

    /**
     * @Author: liuzhaobo
     * @Description: 点击下一题提交本题答案
     * @DateTime: 2022/10/22 0022 13:55
     * @Params:
     * @Return
     */
    @RequestMapping("/insertStuExamSj")
    @ResponseBody
    public ServerResponse<Object> insertStuExamSj(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            Integer ids = entries.getInt("ids");
            Integer examId = entries.getInt("examId");
            Integer sjId = entries.getInt("sjId");
            String optionAnswer = entries.getStr("optionAnswer");
            //先去找到相对应的删除掉
            ksServer.deleteinsertStuExamSj(ids, examId, sjId);

            ksServer.insertStuExamSj(ids, examId, sjId, optionAnswer);
            return ServerResponse.createBySuccessMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("保存失败");
        }
    }

    /**
     * @Author: liuzhaobo
     * @Description: 删除学生试卷
     * @DateTime: 2022/10/22 0022 10:02
     * @Params:
     * @Return
     */
    @RequestMapping("/deleteStuExam")
    @ResponseBody
    public ServerResponse<Object> deleteStuExam(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            Integer stuexamId = entries.getInt("stuexamId");
            ksServer.deleteStuExam(stuexamId);


            return ServerResponse.createBySuccessMsg("删除成功");
        } catch (Exception e) {

            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("删除失败");
        }
    }

    /**
     * @Author: liuzhaobo
     * @Description: 学生点击试卷查看考生的试题
     * @DateTime: 2022/10/25 0025 13:40
     * @Params:
     * @Return
     */
    @RequestMapping("/findStuSJ")
    @ResponseBody
    public ServerResponse<Object> findStuSJ(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            Integer examId = entries.getInt("examId");
            Integer stuexamId = entries.getInt("ids");
            //拿到 试卷id 拿到 考生试卷id
            //查到每一道题

            List<sjVos> findStuSJ = sjServer.findStuSJ(examId);

            //取到 examid 和 stuexamId 和 sj id 寻找 optionAnswer
            for (sjVos as : findStuSJ
            ) {
                Integer sjId = as.getId();
                as.setIds(stuexamId);
                as.setExamIds(examId);


                sjVoss ass = sjServer.findone(examId, stuexamId, sjId);
                if (ass != null) {
                    as.setUseranswer(ass.getOptionAnswer());
                } else {
                    continue;
                }


            }
            //通过 试卷id 找到 name
            //  String ExName = sjServer.selectExName(examId);
            ExamVos exam = sjServer.findExam2(examId);
            sjVoses sjVoses = new sjVoses();
            sjVoses.setSjList(findStuSJ);
            sjVoses.setExamName(exam.getExamName());
            sjVoses.setExamId(examId);
            sjVoses.setStuexamId(stuexamId);
            sjVoses.setTime(exam.getExamTime());
            Date createTime = sjServer.findCreateTime(stuexamId);
            sjVoses.setCreateTime(createTime);

            return ServerResponse.createBySuccess("查询成功", sjVoses);
        } catch (Exception e) {

            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }


    /**
     * @Author: liuzhaobo
     * @Description: 老师点击试卷查看考生的试题
     * @DateTime: 2022/10/25 0025 13:40
     * @Params:
     * @Return
     */
    @RequestMapping("/findTeaSJ")
    @ResponseBody
    public ServerResponse<Object> findTeaSJ(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            Integer examId = entries.getInt("examId");
            Integer stuexamId = entries.getInt("ids");
            //拿到 试卷id 拿到 考生试卷id
            //查到每一道题

            List<sjVos> findStuSJ = sjServer.findStuSJ2(examId);

            //取到 examid 和 stuexamId 和 sj id 寻找 optionAnswer
            for (sjVos as : findStuSJ
            ) {
                Integer sjId = as.getId();
                as.setIds(stuexamId);
                as.setExamIds(examId);
                List<ResourceFj> courseVos = sjServer.getListSjScource(sjId);
                for (ResourceFj asa : courseVos
                ) {
                    asa.setFjurl(Http + asa.getFjurl());

                }

                if (courseVos.size() > 0) {
                    as.setResourceFjs(courseVos);
                }

                sjVoss ass = sjServer.findone(examId, stuexamId, sjId);
                if (ass != null) {
                    as.setUseranswer(ass.getOptionAnswer());
                } else {
                    continue;
                }


            }
            //通过 试卷id 找到 name
            //String ExName = sjServer.selectExName(examId);
            //获取记录时间
            Date outos = sjServer.selectoutos(stuexamId);
            ExamVos exam = sjServer.findExam2(examId);
            sjVoses sjVoses = new sjVoses();
            sjVoses.setSjList(findStuSJ);
            sjVoses.setExamName(exam.getExamName());
            sjVoses.setExamId(examId);
            sjVoses.setStuexamId(stuexamId);
            sjVoses.setCreateTime(exam.getExamCreatetime());
            sjVoses.setTime(exam.getExamTime());
            sjVoses.setUpdateTime(outos);
            return ServerResponse.createBySuccess("查询成功", sjVoses);
        } catch (Exception e) {

            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }

    /**
     * @Author: liuzhaobo
     * @Description: 完成考试
     * @DateTime: 2022/10/25 0025 13:40
     * @Params:
     * @Return
     */
    @RequestMapping("/insertLastStuExamSj")
    @ResponseBody
    public ServerResponse<Object> insertLastStuExamSj(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);

            Integer stuexamId = entries.getInt("ids");

            ksServer.insertLastStuExamSj(stuexamId);

            return ServerResponse.createBySuccess("完成");
        } catch (Exception e) {

            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("失败");
        }
    }

    /**
     * @Author: liuzhaobo
     * @Description: 查看用户信息
     * @DateTime: 2022/10/25 0025 13:40
     * @Params:
     * @Return
     */
    @RequestMapping("/finduserks")
    @ResponseBody
    public ServerResponse<Object> finduserks(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            if (u.getRoleId() == 1 || u.getRoleId() == 2) {
                JSONObject entries = JSONUtil.parseObj(data);
                Integer pageNum = entries.getInt("pageNum");
                Integer pageSize = entries.getInt("pageSize");
                String username = entries.getStr("username");
                PageHelper.startPage(pageNum, pageSize);
                List<User> find = ksServer.finduserks(username);
                PageInfo<User> pageInfo = new PageInfo<User>(find);

                return ServerResponse.createBySuccess(pageInfo);
            } else {
                return ServerResponse.createByErrorMsg("无权限");

            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("失败");
        }
    }


    /**
     * @Author: liuzhaobo
     * @Description: 查看对应学生试题记录
     * @DateTime: 2022/10/25 0025 13:40
     * @Params:
     * @Return
     */
    @RequestMapping("/finduserksExam")
    @ResponseBody
    public ServerResponse<Object> finduserksExam(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            if (u.getRoleId() == 1 || u.getRoleId() == 2) {
                JSONObject entries = JSONUtil.parseObj(data);
                Integer pageNum = entries.getInt("pageNum");
                Integer pageSize = entries.getInt("pageSize");
                String op = entries.getStr("op");
                String px = entries.getStr("px");
                Integer id = entries.getInt("id");
                PageHelper.startPage(pageNum, pageSize);
                List<Ksvos> finduserksExam = ksServer.finduserksExam(op, px, id);
                PageInfo<Ksvos> pageInfo = new PageInfo<Ksvos>(finduserksExam);

                return ServerResponse.createBySuccess(pageInfo);
            } else {
                return ServerResponse.createByErrorMsg("无权限");

            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("失败");
        }
    }
}